
import { _decorator } from "cc";
import CommonToast from "./CommonToast";
import { director } from "cc";
import { Prefab } from "cc";
import { Node } from "cc";
import { instantiate } from "cc";
import { v2 } from "cc";
import { v3 } from "cc";

const { ccclass, property } = _decorator;

@ccclass
export default class CommonToastManager {
    /**
     * 弹窗提示
     * @param  {number} time 弹窗持续的时间 以秒为单位
     * @param  {string} message 弹窗的文字
     */
    public static toast(time: number, message: string, toastPrefab: Prefab) {

        const canvas = director.getScene().getChildByName("Canvas");
        let r = canvas.getChildByName('toastR');
        if (!r) {
            r = new Node();
            r.name = "toastR"
            canvas.addChild(r);
        }

        const toastNode = instantiate(toastPrefab);
        r.addChild(toastNode);
        toastNode.setPosition(v3(0, 0, 0));

        const toastComp = toastNode.getComponent(CommonToast);
        toastComp && toastComp.toast(time, message);
    }
}
